/**
 * 
 */
package com.et114.modules.products.params;

import java.util.List;

import com.et114.components.simpletable.DataList;
import com.et114.core.services.impl.BaseServicesImpl;
import com.et114.core.utility.StringPool;
import com.et114.modules.products.commons.Context;

/*
 * @author guanhw
 */
public class ParamServiceImpl extends BaseServicesImpl {
	private ParamsDao paramsDao ;
	
	public void setParamsDao ( ParamsDao paramsDao ) {
		this.paramsDao = paramsDao;
	}

	public DataList getParamTypes( ParamType vo  ) throws Exception {
		return paramsDao.getParamTypes ( vo ) ; 
	} 
	
	public List< ParamType > getParamTypeList( ParamType vo ) throws Exception {
		return paramsDao.getParamTypeList ( vo ) ; 
	}
	
	public List< ParamType > getRootParamType( ParamType vo ) throws Exception { 
		return paramsDao.getRootParamType ( vo ) ;
	}
	
	public Long saveParamType( ParamType vo ) throws Exception {
		return paramsDao.saveParamType ( vo ) ; 
	}
	
	public ParamType getParamTypeById( Long id ) throws Exception {
		return paramsDao.getParamTypeById ( id ) ; 
	}
	
	public void updateParamType( ParamType vo  )  throws Exception {
		paramsDao.updateParamType ( vo ) ; 
	}
	
	@ SuppressWarnings ( "unchecked" )
	public void removeParamType ( Long id ) throws Exception {
		ParamType pt = getParamTypeById ( id );
		if ( pt != null ) {
			if ( pt.getParentId ( ) == 0 ) { // root
				List < ParamType > sPts = paramsDao.getChildsByTypeId ( id );
				for ( ParamType spt : sPts ) {
					Long sptId = spt.getTypeId ( );
					List < ParamValue > pvs = getParamValuesByTypeId ( sptId );
					for ( ParamValue pv : pvs ) {
						Context.getProPropertySetImp ( ).remove (
								sptId + StringPool.DASH + pv.getParamId ( ) );
					}
				}
			} else { // sub
				List < ParamValue > pvs = getParamValuesByTypeId ( id );
				for ( ParamValue pv : pvs ) {
					Context.getProPropertySetImp ( ).remove (
							id + StringPool.DASH + pv.getParamId ( ) );
				}
			}
		}
		
		paramsDao.removeParamType ( id );
		paramsDao.removeParamTypeByParentId ( id );
		paramsDao.removeParamValueByTypeId ( id );
	}
	
	
	public List < ParamValue > getParamValueList( ParamValue vo ) throws Exception { 
		return paramsDao.getParamValueList ( vo ) ;
	}
	
	public DataList getParamValues( ParamValue vo ) throws Exception {
		return paramsDao.getParamValues ( vo )  ; 
	}
	
	public ParamValue getParamValueById( Long id ) throws Exception {
		return paramsDao.getParamValueById ( id ) ; 
	}
	
	@ SuppressWarnings ( "unchecked" )
	public List getParamValuesByTypeId( Long typeId  ) throws Exception {
		return paramsDao.getParamValuesByTypeId ( typeId ) ;
	}
	
	public Long saveParamValue( ParamValue vo ) throws Exception { 
		return paramsDao.saveParamValue ( vo ) ; 
	}
	
	public void updateParamValue( ParamValue vo ) throws Exception {
		paramsDao.updateParamValue ( vo ) ;
	}	
	
	public void removeParamValue( Long id ) throws Exception {
		ParamValue pv = getParamValueById( id ) ; 
		if ( pv != null  ) {
			Long typeId = pv.getTypeId ( ) ;
			Context.getProPropertySetImp ( ).remove (
					typeId + StringPool.DASH + id );
		}
		paramsDao.removeParamValue ( id ) ; 
	}
	/*
	public List< ParamValue > getParamValuesByType( ParamValue vo ) throws Exception {
		return paramsDao.getParamValuesByType ( vo ) ;
	}	*/
	
	
	

}
